package com.scalar.chiptrack.operations.dataentry.actions;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.operations.process.dao.LotDAO;
import com.scalar.chiptrack.operations.dataentry.dao.TransitDataEntryDAO;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.users.User;
import com.scalar.ScalarException;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.StringTokenizer;
import java.util.ArrayList;
import java.util.HashMap;


public class PullLotNumbersAction extends ChipTrackAction
{
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        HashMap errors  = new HashMap();
        HttpSession session = request.getSession();
        User user   = (User)session.getAttribute( SessionKeys.USER_KEY );

        if ( user == null )
        {
            request.setAttribute ( "closeWindowAfterSessionExp", "true" );
			return ( mapping.findForward( "success" ) );
        }

        String invType          = request.getParameter( "type" );
        String locationIdStr    = request.getParameter( "locationId" );
        String processStepIdStr = request.getParameter ( "processStepId" );
        String actionForward    = "success";

        StringTokenizer someValue = new StringTokenizer( processStepIdStr,"$" );
        String processStepId = someValue.nextToken();
        ArrayList lotNumbers    = null;
        if ( invType != null && invType.startsWith( "wafer" ) ) //Wafer Store
        {
            try
            {
                lotNumbers = LotDAO.getTransitLotNumbers ( null, processStepId );
            }
            catch( ScalarException e )
            {
                errors.put ( "Could not get the Transit Lot Numbers",null );
            }
        }
        else if ( invType != null && invType.startsWith( "class" ) ) //Class Store
        {
            try
            {
                lotNumbers = LotDAO.getTestTransitLotNumbers ( null, processStepId );
            }
            catch( ScalarException e )
            {
                errors.put ( "Could not get the Test Transit Lot Numbers",null );
            }
        }
        else if ( invType != null && invType.equalsIgnoreCase("FAB") && locationIdStr != null ) //Class Store
        {
            try
            {
                lotNumbers = TransitDataEntryDAO.getFabLotNumbers( processStepId, locationIdStr, "FAB");
            }
            catch( ScalarException e )
            {
                errors.put ( "Could not get the FAB Lot Numbers",null );
            }
        }
        else if ( invType != null && invType.equalsIgnoreCase("TEST") && locationIdStr != null ) //Class Store
        {
            try
            {
                lotNumbers = TransitDataEntryDAO.getTestLotNumbers( processStepId, locationIdStr, "TEST");
            }
            catch( ScalarException e )
            {
                errors.put ( "Could not get the Test Lot Numbers",null );
            }
        }

        request.setAttribute ( "type", invType );
        request.setAttribute ( "lotNumbers", lotNumbers );
        if( invType.equalsIgnoreCase("FAB") || invType.equalsIgnoreCase("TEST") )
        {

           actionForward = "showLots";

        }
        return mapping.findForward( actionForward );
    }
}
